'use strict';
const Service = require('egg').Service;

module.exports = class CommonService extends Service {
    getModel(name) {
        const { ctx } = this;
        var s = name.substr(0, 1).toUpperCase() + name.substr(1);
        if (ctx.model[s]) {
            return ctx.model[s];
        }
        for (let i = 0; i < ctx.helper.models.length; ++i) {
            let name = ctx.helper.models[i];
            if (ctx.model[name] && ctx.model[name][s]) {
                return ctx.model[name][s];
            }
        }
        return null;
    }



    getServer(s) {
        const { ctx } = this;
        if (ctx.service[s]) {
            return ctx.service[s];
        }
        let i = s.indexOf("_");
        for (; i != -1; i = s.indexOf("_")) {
            s = s.substring(0, i) + s[i + 1].toUpperCase() + s.substring(i + 2);
            if (ctx.service[s]) {
                return ctx.service[s];
            }
        }
        for (let i = 0; i < ctx.helper.services.length; ++i) {
            let name = ctx.helper.services[i];
            if (ctx.service[name] && ctx.service[name][s]) {
                return ctx.service[name][s];
            }
        }
        return null;
    }


    // 获取列表
    async get_list(model, query, body, ser) {
        try {
            const { ctx } = this;
            // 判断model里是否有条件
            // console.log(model, model.name, "====")
            // var ser = this.getServer(model.name);
            if (!ser && model) {
                ser = this.getServer(model.name);
            }
            var condition = { order: [] };
            // ['created_at']
            for (let a in query) {
                if (a.indexOf("sort_") == 0) {
                    let key = a.substr("sort_".length);
                    key = key.replace(".name", "_id");
                    key = key.replace(".value", "_id");
                    condition.order.push([key, query[a] == 1 ? 'desc' : 'asc'])
                }
            }
            if (ser && ser.listCondition instanceof Function) {
                condition = (await ser.listCondition.apply(ser, [model, query, condition, body])) || condition;
            }
            // 处理分页
            if (query.pageIndex !== undefined && query.pageSize !== undefined) {
                condition.offset = parseInt((query.pageIndex) * query.pageSize);
                condition.limit = parseInt(query.pageSize);
            }


            // 查找数据
            let data;
            if (model.use_d4) {
                data = await model.d4_listCount(ctx, query, condition);
            } else {
                data = await model.findAndCountAll(condition);
            }
            // 返回前数据处理
            if (ser && ser.afterList instanceof Function) {
                data = (await ser.afterList.apply(ser, [model, query, ctx.helper.clone(data), data, body])) || data;
            }

            return { total: data.count, data: data.rows };
        } catch (e) {
            console.log(e, "-----------get_list----------")
        }
    }

    async s(dat) {
        return { data: dat };
    }

}